﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class Admin_Code : System.Web.UI.Page
{
    private void Page_Load(object sender, EventArgs e)
    {
        string vNum = this.RndNum(4);
        this.Session["code"] = vNum;
        this.ValidateCode(vNum);
    }

    public string RndNum(int VcodeNum)
    {
        string[] strArray = "0,1,2,3,4,5,6,7,8,9".Split(new char[] { ',' });
        string str2 = "";
        int num = -1;
        Random random = new Random();
        for (int i = 1; i < (VcodeNum + 1); i++)
        {
            if (num != -1)
            {
                random = new Random((i * num) * ((int)DateTime.Now.Ticks));
            }
            int index = random.Next(9);
            if ((num != -1) && (num == index))
            {
                return this.RndNum(VcodeNum);
            }
            num = index;
            str2 = str2 + strArray[index];
        }
        return str2;
    }

    private void ValidateCode(string VNum)
    {
        int width = (int)(VNum.Length * 9.5);
        Bitmap image = new Bitmap(width, 0x12);
        Graphics graphics = Graphics.FromImage(image);
        graphics.DrawString(VNum, new Font("宋体", 12f), new SolidBrush(Color.Red), (float)0f, (float)0f);
        MemoryStream stream = new MemoryStream();
        image.Save(stream, ImageFormat.Png);
        base.Response.ClearContent();
        base.Response.ContentType = "image/Png";
        base.Response.BinaryWrite(stream.ToArray());
        graphics.Dispose();
        image.Dispose();
        base.Response.End();
    }
}
